<template>
  <!-- <div> -->
  <fieldset class="userAdd">
    <legend>新增用户</legend>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="姓名" class="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="性別" class="sex">
        <el-radio-group v-model="form.sex">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="生日">
        <el-col :span="11">
          <el-date-picker
            type="date"
            format="yyyy-MM-dd"
            placeholder="选择日期"
            v-model="form.birthday"
            style="width: 100%"
            :editable="false"
          ></el-date-picker>
        </el-col>
      </el-form-item>
      <el-form-item label="地址" class="username">
        <el-input v-model="form.address"></el-input>
      </el-form-item>
      <el-form-item label="部门" class="dept">
        <el-select v-model="form.deptId" placeholder="请选择部门">
          <el-option
            v-for="(item, index) in deptList"
            :key="index"
            :label="item.deptName"
            :value="item.deptId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button @click="onEmpty">重置</el-button>
      </el-form-item>
    </el-form>
  </fieldset>
  <!-- </div> -->
</template>
<script>
export default {
  data() {
    return {
      form: {
        username: "",
        sex: "",
        birthday: "",
        deptId: "",
        address: "",
        message: "",
      },
      deptList: [],
    };
  },
  mounted() {
    this.findDeptAll();
  },
  methods: {
    findDeptAll() {
      let _this = this;
      this.$axios({
        url: this.$apiServer.findAllDept,
        methods: "get",
      }).then(function (res) {
        console.log(res);
        _this.deptList = res.data;
      });
    },
    onSubmit() {
      console.log(this.form);
      let _this = this;
      let data = this.form;
      this.$axios({
        method: "post",
        url: _this.$apiServer.addUser,
        data,
      }).then(function (res) {
        if (res.data.code == "200") {
          this.$message({
            type: "success",
            message: "添加成功!",
          });
          _this.form = {};
        } else {
          _this.$message({
            type:"error",
            message:"添加失败！"
          });
        }
      });
    },
    onEmpty() {
      this.form = {};
    },
  },
};
</script>
<style>
@import url("/css/userAdd.css");
</style>